package com.sunda.spmsorder.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sunda.spmsorder.entity.ShoppingScenario;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 请购场景表表头 Mapper 接口
 * </p>
 *
 * @author Wayne
 * @since 2021-03-01
 */
@Mapper
@Component
public interface ShoppingScenarioMapper extends BaseMapper<ShoppingScenario> {

    @Select("<script>" +
            "SELECT SS.UUID AS \"uuid\",\n" +
            "       SS.SCENARIO_ID AS \"scenarioId\",\n" +
            "       SS.WERKS AS \"werks\",\n" +
            "       SS.SCENARIO_NAME AS \"scenarioName\",\n" +
            "       SS.SCENARIO_DESC AS \"scenarioDesc\",\n" +
            "       SS.MACHINE_NO AS \"machineNo\",\n" +
            "       SS.MACHINE_DESC AS \"machineDesc\",\n" +
            "       SS.REMARKS AS \"remarks\",\n" +
            "       SS.IS_DELETE AS \"isDelete\",\n" +
            "       SS.CREATE_USERID AS \"createUserid\",\n" +
            "       SU.LAST_NAME AS \"createUserName\",\n" +
            "       SS.UPDATE_USERID AS \"updateUser\",\n" +
            "       SU1.LAST_NAME AS \"updateUserName\",\n" +
            "       SS.CREATE_TIME AS \"createTime\",\n" +
            "       SS.UPDATE_TIME AS \"updateTime\"\n" +
            "FROM SHOPPING_SCENARIO SS \n" +
            "    LEFT JOIN SPMS_USER SU ON SS.CREATE_USERID = SU.WORK_CODE \n" +
            "    LEFT JOIN SPMS_USER SU1 ON SS.UPDATE_USERID = SU1.WORK_CODE\n" +
            "WHERE SS.WERKS IN \n" +
            "    <foreach item='item' index='index' collection='werks' open='(' separator=',' close=')'>\n" +
            "        #{item}\n" +
            "    </foreach>" +
            "<when test='isDelete!=null'> AND SS.IS_DELETE = #{isDelete} </when>\n" +
            "    ORDER BY SS.CREATE_TIME DESC \n" +
            "</script>")
    List<Map<String, Object>> getScenarioList(@Param("werks") String [] werks, @Param("isDelete") String isDelete);

    @Select("<script>" +
            "SELECT SS.UUID AS \"uuid\",\n" +
            "       SS.SCENARIO_ID AS \"scenarioId\",\n" +
            "       SS.WERKS AS \"werks\",\n" +
            "       SS.SCENARIO_NAME AS \"scenarioName\",\n" +
            "       SS.SCENARIO_DESC AS \"scenarioDesc\",\n" +
            "       SS.MACHINE_NO AS \"machineNo\",\n" +
            "       SS.MACHINE_DESC AS \"machineDesc\",\n" +
            "       SS.REMARKS AS \"remarks\",\n" +
            "       SS.IS_DELETE AS \"isDelete\",\n" +
            "       SS.CREATE_USERID AS \"createUserid\",\n" +
            "       SU.LAST_NAME AS \"createUserName\",\n" +
            "       SS.UPDATE_USERID AS \"updateUser\",\n" +
            "       SU1.LAST_NAME AS \"updateUserName\",\n" +
            "       SS.CREATE_TIME AS \"createTime\",\n" +
            "       SS.UPDATE_TIME AS \"updateTime\"\n" +
            "FROM SHOPPING_SCENARIO SS \n" +
            "    LEFT JOIN SPMS_USER SU ON SS.CREATE_USERID = SU.WORK_CODE \n" +
            "    LEFT JOIN SPMS_USER SU1 ON SS.UPDATE_USERID = SU1.WORK_CODE\n" +
            "WHERE 1=1 \n" +
            "<when test='isDelete!=null'> AND SS.WERKS IN \n" +
            "    <foreach item='item' index='index' collection='werks' open='(' separator=',' close=')'>\n" +
            "        #{item}\n" +
            "    </foreach>" +
            " </when>\n" +
            "<when test='isDelete!=null'> AND SS.IS_DELETE = #{isDelete} </when>\n" +
            "<when test='spmsId!=null'> AND SS.SCENARIO_ID = #{spmsId} </when>\n" +
            "<when test='authorName!=null'> AND SU.LAST_NAME LIKE concat(concat('%',#{authorName}),'%') </when>\n" +
            "<when test='scenarioName!=null'> AND SS.SCENARIO_NAME LIKE concat(concat('%',#{scenarioName}),'%') </when>\n" +
            "    ORDER BY SS.CREATE_TIME DESC \n" +
            "</script>")
    List<Map<String, Object>> queryScenarioList(
            @Param("werks") String werks[], 
            @Param("isDelete") String isDelete,
            @Param("authorName") String authorName,
            @Param("scenarioName") String scenarioName,
            @Param("spmsId") String spmsId);


    @Update("<script> UPDATE SHOPPING_SCENARIO SET IS_DELETE = 1 WHERE UUID IN \n" +
            "    <foreach item='item' index='index' collection='scenarioUuid' open='(' separator=',' close=')'>\n" +
            "        #{item}\n" +
            "    </foreach>" +
            "</script>")
    int deleteScenarios(@Param("scenarioUuid") String [] scenarioUuid);
}
